/*
 * Copyright 2009 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.core.catalog;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.kommando.core.util.Assert;

/**
 * TODO add enable/disable functionality objectsources/individual objects
 * 
 * @author Peter De Bruycker
 */
public class DefaultCatalog implements Catalog {

    private List<CatalogObject> cachedObjects = new ArrayList<CatalogObject>();
    private List<ObjectSource> objectSources;

    public DefaultCatalog(List<ObjectSource> objectSources) {
        Assert.argumentNotNull("objectSources", objectSources);

        this.objectSources = objectSources;
    }

    public List<CatalogObject> getObjects() {
        List<CatalogObject> objects = new ArrayList<CatalogObject>(cachedObjects);

        for (ObjectSource objectSource : objectSources) {
            if (!(objectSource instanceof CachedObjectSource)) {
                objects.addAll(getObjects(objectSource));
            }
        }

        for (CatalogObject catalogObject : objects) {
            enhance(catalogObject);
        }

        return objects;
    }

    // TODO keep enhancing for the added extensions?
    public void enhance(CatalogObject object) {
        Map<Class<?>, Object> extensions = new HashMap<Class<?>, Object>(object.getExtensions());

        for (ObjectSource objectSource : objectSources) {
            Class<?>[] extensionTypes = objectSource.getExtensionTypes(object);
            if (extensionTypes != null && extensionTypes.length > 0) {
                for (Class extensionType : extensionTypes) {
                    Object extension = objectSource.getExtension(object, extensionType);
                    object.setExtension(extensionType, extension);
                }
            }
        }

        if (!extensions.entrySet().equals(object.getExtensions().entrySet())) {
            enhance(object);
        }
    }

    private List<CatalogObject> getObjects(ObjectSource objectSource) {
        Assert.argumentNotNull("objectSource", objectSource);

        List<CatalogObject> objects = objectSource.getObjects();
        if (objects == null) {
            throw new IllegalStateException("getObjects method of " + objectSource + " returns null");
        }

        return objects;
    }

    public CatalogObject getObject(String id) {
        for (CatalogObject object : getObjects()) {
            if (id.equals(object.getId())) {
                return object;
            }
        }

        for (ObjectSource objectSource : objectSources) {
            CatalogObject object = objectSource.getObject(id);
            if (object != null) {
                enhance(object);
                return object;
            }
        }

        return null;
    }

    public void index() {
        System.out.println("Start indexing");
        System.out.println("ObjectSources:");
        for (ObjectSource objectSource : objectSources) {
            System.out.println(" - " + objectSource);
        }

        List<CatalogObject> objects = new ArrayList<CatalogObject>();
        for (ObjectSource objectSource : objectSources) {
            if (objectSource instanceof CachedObjectSource) {
                objects.addAll(getObjects(objectSource));
            }
        }

        synchronized (cachedObjects) {
            cachedObjects.clear();
            cachedObjects.addAll(objects);
        }

        System.out.println("Finished indexing");
    }

    public List<CatalogObject> getChildren(CatalogObject object) {
        List<CatalogObject> children = new ArrayList<CatalogObject>();

        for (ObjectSource objectSource : objectSources) {
            if (objectSource.hasChildren(object)) {
                List<CatalogObject> results = objectSource.getChildren(object);
                if (results != null) {
                    for (CatalogObject childObject : results) {
                        enhance(childObject);
                    }
                    children.addAll(results);
                }
            }
        }
        return children;
    }

    public boolean hasChildren(CatalogObject object) {
        for (ObjectSource objectSource : objectSources) {
            if (objectSource.hasChildren(object)) {
                return true;
            }
        }

        return false;
    }

    public List<ObjectSource> getObjectSources() {
        return Collections.unmodifiableList(objectSources);
    }
}
